package dyyx.batch;

import org.apache.flink.api.common.operators.Order;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.functions.KeySelector;

public class SimpleTest {

	public static void main(String[] args) throws Exception {
		
		final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
		 
	    System.out.println(env);	
	    System.out.println("parallelism="+env.getParallelism());	

	    env.setParallelism(1);
	    
	    System.out.println("parallelism2="+env.getParallelism());	

        DataSet<String> text = env.fromElements(
            "hello","cat","dog","cat","hello","cat","dog","dog","cat"
        );
        
        
        
       long count = text.distinct().count();
        
       System.out.println(count);
       
       // sort
       System.out.println("sort print");

       text.sortPartition(new MyKeySelector(), Order.ASCENDING).print();;
       
       System.out.println("sort firstn print");
       // 
       text.sortPartition(new MyKeySelector(), Order.ASCENDING).first(7).print();

       //
       System.out.println("groupBy firstn(1) print");

       text.groupBy(new MyKeySelector()).first(1).print();
       
       System.out.println("groupBy firstn(2) print");
       text.groupBy(new MyKeySelector()).first(2).print();
       
       
       System.out.println("groupBy getKeys toString");
       String keys = text.groupBy(new MyKeySelector()).getKeys().toString();
       System.out.println("keys="+keys);

       


	}
	
	
	private static class MyKeySelector  implements KeySelector<String,String>{
		public String getKey(String value) {
			return value;
		}
	}

}
